package com.wenc.psi.fgmt;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.wenc.psi.R;
import com.wenc.psi.bean.SaleBean;
import com.wenc.psi.databinding.FragmentStatisticsBinding;
import com.wenc.psi.pojo.Sale;
import com.wenc.psi.rvAdapter.SaleOrderAdapter;
import com.wenc.psi.vmd.SaleOrderViewModel;

import java.util.LinkedList;
import java.util.List;

public class StatisticsFragment extends Fragment {

    private SaleOrderViewModel saleOrderViewModel;
    private FragmentStatisticsBinding binding;
    private SaleOrderAdapter saleOrderAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_statistics, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // 创建适配器
        saleOrderAdapter = new SaleOrderAdapter();
        // 设置recyclerView
        binding.rvSaleOrder.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.rvSaleOrder.setAdapter(saleOrderAdapter);
        // 获取ViewModel
        saleOrderViewModel = new ViewModelProvider(getActivity()).get(SaleOrderViewModel.class);
        // 设置数据监听
        saleOrderViewModel.getSaleLive().observe(getActivity(), new Observer<List<Sale>>() {
            @Override
            public void onChanged(List<Sale> sales) {
                saleOrderAdapter.setSaleList(sales);
                saleOrderAdapter.notifyDataSetChanged();
            }
        });
    }

}